package com.tianyang.yukuaifu.business.login.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.jakewharton.rxbinding.view.RxView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseEditActivity;
import com.tianyang.yukuaifu.business.login.contract.RegisterContract;
import com.tianyang.yukuaifu.business.login.presenter.RegisterPresenter;
import com.tianyang.yukuaifu.business.main.provider.MainOpenProvider;
import com.tianyang.yukuaifu.common.interfaces.UrlConstants;
import com.tianyang.yukuaifu.module.specialunit.OnClickableSpanListener;
import com.tianyang.yukuaifu.module.specialunit.SimplifySpanBuild;
import com.tianyang.yukuaifu.module.specialunit.unit.SpecialClickableUnit;
import com.tianyang.yukuaifu.module.widget.EditTextWithDel;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Description：注册
 */
public class RegisterActivity extends BaseEditActivity implements RegisterContract.View {

    @BindView(R.id.etPhone)
    EditTextWithDel etPhone;
    @BindView(R.id.etCode)
    EditTextWithDel etCode;
    @BindView(R.id.etPwd)
    EditTextWithDel etPwd;
    @BindView(R.id.etConfirmPwd)
    EditTextWithDel etConfirmPwd;
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;
    @BindView(R.id.etShareCode)
    EditText etShareCode;

    @BindView(R.id.legal)
    TextView vLegal;
    @BindView(R.id.chooseIv)
    ImageView chooseIv;

    private RegisterContract.Presenter mPresenter;

    private boolean isCheckAgree;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("用户注册");
        SpecialClickableUnit clickableUnit = new SpecialClickableUnit(new OnClickableSpanListener() {
            @Override
            public void onClick(TextView tv, String clickText) {
                MainOpenProvider.getInstance().openBrowserX5Activity(mContext, "《隐私政策》", UrlConstants.PRIVACY_POLICY);
            }
        });
        clickableUnit.setSpecialText("《隐私政策》");
        clickableUnit.setNormalTextColor(ContextCompat.getColor(mContext, R.color.color_base));
        clickableUnit.setPressTextColor(ContextCompat.getColor(mContext, R.color.base_press_color));

        SpecialClickableUnit clickableUnit2 = new SpecialClickableUnit(new OnClickableSpanListener() {
            @Override
            public void onClick(TextView tv, String clickText) {
                MainOpenProvider.getInstance().openBrowserX5Activity(mContext, "服务商合规展业规范", UrlConstants.AGREEMENT);
            }
        });
        clickableUnit2.setSpecialText("《服务商合规展业规范》");
        clickableUnit2.setNormalTextColor(ContextCompat.getColor(mContext, R.color.color_base));
        clickableUnit2.setPressTextColor(ContextCompat.getColor(mContext, R.color.base_press_color));

        SimplifySpanBuild build = new SimplifySpanBuild(this, vLegal, "已充分阅读并遵守");
        build.appendSpecialUnit(clickableUnit);
        build.appendSpecialUnit(clickableUnit2);
        build.appendNormalText("确保您的个人的权益得到保护。");
        vLegal.setText(build.build());

        mPresenter = new RegisterPresenter(this);
    }

    @Override
    protected void setListener() {
        super.setListener();
        RxView.clicks(tvGetCode).throttleFirst(3, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (!isRespondEvent()) {
                            return;
                        }
                        mPresenter.sendCode(String.valueOf(etPhone.getText()));
                    }
                });
    }

    @OnClick({R.id.registerTv, R.id.chooseIv})
    public void onClick(View view) {
        if (!isRespondEvent()) return;
        switch (view.getId()) {
            case R.id.registerTv:
                if (!checkParameter(false)) return;
                mPresenter.register(etPhone.getText().toString(), etCode.getText().toString(),
                        etPwd.getText().toString(), etShareCode.getText().toString());
                break;
            case R.id.chooseIv:
                if (isCheckAgree) {
                    isCheckAgree = false;
                    chooseIv.setImageResource(R.drawable.ic_choose_normal);
                } else {
                    isCheckAgree = true;
                    chooseIv.setImageResource(R.drawable.ic_choose_checked);
                }
                break;
        }
    }

    @Override
    public void byoyomi(Long aLong) {
        tvGetCode.setText(aLong + "秒");
    }

    @Override
    public void changeByStatus(boolean status) {
        if (status) {
            tvGetCode.setEnabled(false);
            tvGetCode.setTextColor(
                    ContextCompat.getColor(mContext, R.color.c_999999));
        } else {
            tvGetCode.setText("获取验证码");
            tvGetCode.setEnabled(true);
            tvGetCode.setTextColor(
                    ContextCompat.getColor(mContext, R.color.color_base));
        }
    }

    @Override
    public boolean checkParameter(boolean checkCode) {
        String userName = etPhone.getText().toString();
        String code = etCode.getText().toString();
        String password = etPwd.getText().toString();
        String confirmPwd = etConfirmPwd.getText().toString();
        String shareCode = etShareCode.getText().toString();
        if (TextUtils.isEmpty(userName)) {
            ToastUtil.showTextViewPromptShort("请输入您的手机号");
            SoftInputUtil.openKeybord(etPhone, mContext);
            return false;
        } else if (VerifyUtil.checkPhoneNumber(userName)) {
            ToastUtil.showTextViewPromptShort("电话号码输入不正确");
            SoftInputUtil.openKeybord(etPhone, mContext);
            return false;
        }
        if (!checkCode) {
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showTextViewPromptShort("请输入验证码");
                SoftInputUtil.openKeybord(etCode, mContext);
                return false;
            }
            if (code.length() < 4) {
                ToastUtil.showTextViewPromptShort("请输入4位验证码");
                SoftInputUtil.openKeybord(etCode, mContext);
                return false;
            }
            if (TextUtils.isEmpty(password)) {
                ToastUtil.showTextViewPromptShort("请输入密码");
                SoftInputUtil.openKeybord(etPwd, mContext);
                return false;
            }
            if (password.length() < 6) {
                ToastUtil.showTextViewPrompt("账号密码不能小于6位数");
                SoftInputUtil.openKeybord(etPwd, mContext);
                return false;
            }
            if (TextUtils.isEmpty(confirmPwd)) {
                ToastUtil.showTextViewPromptShort("请再次输入密码");
                SoftInputUtil.openKeybord(etConfirmPwd, mContext);
                return false;
            }
            if (!TextUtils.equals(password, confirmPwd)) {
                ToastUtil.showTextViewPrompt("两次密码输入不一致，请重新输入");
                SoftInputUtil.openKeybord(etConfirmPwd, mContext);
                return false;
            }
            if(TextUtils.isEmpty(shareCode)){
                ToastUtil.showTextViewPromptShort("请输入邀请码");
                return false;
            }
            if (!isCheckAgree) {
                ToastUtil.showTextViewPromptShort("请阅读并勾选《隐私政策》及《服务商合规展业规范》");
                SoftInputUtil.hideSoftInput(this);
                return false;
            }
        }
        return true;
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(RegisterContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
